<?php


namespace App\Http\Controllers\Weapp;


use App\Tool\Json;

class Address extends Auth
{



    function saveAddress(){


        try{
            \DB::beginTransaction();

            $post=request()->input();

            $post['user_id']=resolve('weapp_user')['id'];


            if($post['is_default']==1) \App\Model\Address::where('user_id',$post['user_id'])->update(['is_default'=>2]);

            $data=\App\Model\Address::updateOrCreate(['id'=>request()->input('id')],$post);


            \DB::commit();

            return Json::code(1,'success',$data);


        }catch (\Exception $exception){

            \DB::rollBack();


            return Json::code(1,$exception->getMessage());

        }


    }


    function addressList(){

        $user_id=resolve('weapp_user')['id'];

        $list=\App\Model\Address::where('user_id',$user_id)->orderBy('id','desc')->get();


        return Json::code(1,'success',$list);

    }


    function getDetail(){

        $id=request()->input('id');

        $data=\App\Model\Address::find($id);

        return Json::code(1,'success',$data);

    }


    function deleteAddress(){

        $id=request()->input('id');

        $user_id=resolve('weapp_user')['id'];


        \App\Model\Address::where('id',$id)->where('user_id',$user_id)->delete();


        return Json::code(1,'success');

    }

}
